﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using DataContracts;
using System.ServiceModel;
using System.Linq;
using System.Web;


namespace TrainingWorkshop.MVPWebsite.User.Views
{
    public class UpdateUserPresenter : Presenter<IUpdateUserView>
    {

        // NOTE: Uncomment the following code if you want ObjectBuilder to inject the module controller
        //       The code will not work in the Shell module, as a module controller is not created by default
        //
        // private IUserController _controller;
        // public UpdateUserPresenter([CreateNew] IUserController controller)
        // {
        // 		_controller = controller;
        // }


        private IUserController _usercontroller;
        public UpdateUserPresenter([CreateNew] IUserController usercontroller)
        {
            _usercontroller = usercontroller;
        }
        public override void OnViewLoaded()
        {
            // TODO: Implement code that will be executed every time the view loads
        }

        public override void OnViewInitialized()
        {
            // TODO: Implement code that will be executed the first time the view loads
            View.Departments = _usercontroller.GetAllDepartment();
            View.Roles = _usercontroller.GetAllUserRole();

            string uid = (View.UserId).ToString();
            if (uid == null)
            {
                View.ShowOperationMessage("Failed.");

            }
            else
            {
                int userid = int.Parse(uid);
                try
                {
                    UserDTO User = _usercontroller.GetUserById(userid);
                    View.FirstName = User.FirstName;
                    View.LastName = User.LastName;
                    View.DepartmentId = User.Department.DepartmentId;
                    View.RoleId = User.Role.RoleId;
                }
                catch (FaultException ex)
                {
                    View.ShowOperationMessage(ex.Message);
                    View.SkipToAnotherPage("UserList.aspx");
                }
 
            }
        }

        // TODO: Handle other view events and set state in the view

        public void OnUpdateUser()
        {
            
            UserDTO User = new UserDTO();
            User.UserId = View.UserId;
            User.FirstName = View.FirstNameUpdate;
            User.LastName = View.LastNameUpdate;
            DepartmentDTO Department = new DepartmentDTO();
            Department.DepartmentId = View.DepartmentIdUpdate;
            Department.DepartmentName = View.DepartmentNameUpdate;
            User.Department = Department;
            RoleDTO Role = new RoleDTO();
            Role.RoleId = View.RoleIdUpdate;
            Role.RoleName = View.RoleNameUpdate;
            StatusDTO Status = new StatusDTO();
            Status.StatusId = 1;
            Status.StatusName = "InUse";
            Role.Status = Status;
            User.Role = Role;
            
            try
            {
                if (User != null)
                {
                    _usercontroller.UpdateUser(User);
                   
                    View.SkipToAnotherPage("UserList.aspx");
                }
                else
                {
                    View.ShowOperationMessage("Failed.");
 
                }

            }
            catch (FaultException ex)
            {
                if (ex.Code.Name == "User Deleted")
                {
                    View.ShowOperationMessage(ex.Message);
                    View.SkipToAnotherPage("UserList.aspx");
                }
                if (ex.Code.Name == "Role Deleted")
                {
                    View.ShowOperationMessage(ex.Message);
                    View.Roles = _usercontroller.GetAllUserRole();
                }
            }
 
        }
       
    }
}




